﻿using System;
using System.Data;
using System.Collections;

public partial class Simple : System.Web.UI.Page
{
    public Data.VarParams varParam = new Data.VarParams();
    public Data.Data currentData = new Data.Data();
    public Data.DataConsolidation dc = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        String errMsg = varParam.ReadFile(Request.PhysicalApplicationPath + "DemoData\\", this.Session.SessionID);
        if (errMsg == null)
        {
            dc = new Data.DataConsolidation(varParam);

            String projectFileName = "CombineDuplicates SwapTextColumnWithRowLabel DeleteTextColumn.txt";
            Boolean error = ExecuteProjectFile( projectFileName );
            if (error == false)
            {
                Data.Page currentPage = currentData.GetSelectedPage();
                if (currentPage == null)
                    return;

                DataSet dataset = makeDataTable(currentPage);
                if (dataset != null)
                {
                    GridView1.DataSource = dataset;
                    GridView1.DataBind();
                }
            }
        }
    }
    Boolean ExecuteProjectFile( String projectFileName )
    {
        String fullProjectFileName = varParam.GetMyProjectsDir() + projectFileName;
        if ( System.IO.File.Exists(fullProjectFileName))
        {
            String [] projectItems = System.IO.File.ReadAllLines( fullProjectFileName );

            foreach( String projectItem in projectItems )
            {
                Boolean initialLoad = true;
                Boolean success = dc.RunScript(projectItem, currentData, initialLoad);
                if (success == false)
                    return success;
            }
            return false;
        }
        return false;
    }

    private DataSet makeDataTable( Data.Page currentPage )
    {
        DataSet dataset = new DataSet();
        if (dataset == null)
            return null;
        
        DataTable thisTable = dataset.Tables.Add( currentPage.GetPageCaption() );

        DataColumn rowLabel = thisTable.Columns.Add();
        rowLabel.ColumnName = currentPage.GetRowLabelName();
        rowLabel.DataType = typeof(String);

        int textCount = currentPage.GetTextColumnCount();
        for (int textIndex = 0; textIndex < textCount; textIndex++)
        {
            DataColumn textColumn = thisTable.Columns.Add();
            textColumn.ColumnName = currentPage.GetTextColumnName(textIndex);
            textColumn.DataType = typeof(String);
        }

        int colCount = currentPage.GetNumericColumnCount();
        for (int colIndex = 0; colIndex < colCount; colIndex++)
        {
            DataColumn doubleColumn = thisTable.Columns.Add();
            doubleColumn.ColumnName = currentPage.GetNumericColumnName(colIndex);
            doubleColumn.DataType = typeof(Double);
        }

        int totalColumnCount = 1 + textCount + colCount;
        for (int rowIndex = 0, rowCount = currentPage.GetRowCount();
                    rowIndex < rowCount; rowIndex++)
        {
            object[] fields = new object[totalColumnCount];
            int fieldIndex = 0;

            fields[fieldIndex++] = currentPage.GetRowLabelValue(rowIndex);
            for (int textIndex = 0; textIndex < textCount; textIndex++)
            {
                fields[fieldIndex++] = currentPage.GetTextColumnValue(rowIndex, textIndex);
            }            
            for (int colIndex = 0; colIndex < colCount; colIndex++)
            {
                Double dVal = currentPage.GetNumericCellContents( rowIndex, colIndex );
                if (dVal == Data.Data.BadNum() )
                    fields[fieldIndex++] = null;
                else
                    fields[fieldIndex++] = dVal;
            }            
            DataRow currentRow = thisTable.Rows.Add(fields);
        }
        return dataset;
    }
}



